#include <fcntl.h> 
#include <stdio.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <sys/types.h> 
#include <time.h> 
#include <unistd.h> 
 
 // http://www.makelinux.net/alp/098
/* Return a character string representing the current date and time.  */ 
// http://stackoverflow.com/questions/18415904/what-does-mode-t-0644-mean
/*
rwx oct   meaning
--- ---   -------
001 1   = execute
010 2   = write
011 3   = write & execute
100 4   = read
101 5   = read & execute
110 6   = read & write
111 7   = read & write & execute

So 0644 is:
* (owning) User: read & write
* Group: read
* Other: read
This might also be written:

-rw-r--r--
Whereas full permissions, 0777 cam also be written:
-rwxrwxrwx
*/
char* get_timestamp () 
{
  time_t now = time (NULL); 
  return asctime (localtime (&now)); 
} 
 
int main (int argc, char* argv[]) 
{
  /* The file to which to append the timestamp.  */ 
  char* filename = argv[1]; 
  /* Get the current timestamp.  */ 
  char* timestamp = get_timestamp (); 
  /* Open the file for writing. If it exists, append to it; 
     otherwise, create a new file.  */ 
  int fd = open (filename, O_WRONLY | O_CREAT | O_APPEND, 0666); 
  /* Compute the length of the timestamp string.  */ 
  size_t length = strlen (timestamp); 
  /* Write the timestamp to the file.  */ 
  write (fd, timestamp, length); 
  /* All done.  */ 
  close (fd); 
  return 0; 
} 